/*
 *  Copyright (c) 2010 Brian Stanek <bstanek@gmail.com>
 *
 *  Permission is hereby granted, free of charge, to any person
 *  obtaining a copy of this software and associated documentation
 *  files (the "Software"), to deal in the Software without
 *  restriction, including without limitation the rights to use,
 *  copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following
 *  conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *  OTHER DEALINGS IN THE SOFTWARE.
 */

package guice.binder
{

import guice.Binder;
import guice.BindingBuilder;
import guice.Stage;
import guice.injector.InjectorImpl;
import guice.spi.Element;
import guice.spi.Message;

public class BinderImpl implements Binder
{

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    public function BinderImpl(injector:InjectorImpl, stage:Stage=null)
    {
        super();
        this.injector = injector;
        this.stage = stage;
    }

    //--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------

    private var elements:Vector.<Element> = new Vector.<Element>();

    private var injector:InjectorImpl;

    private var source:Object;

    private var stage:Stage;

    //--------------------------------------------------------------------------
    //
    //  Methods: Binder
    //
    //--------------------------------------------------------------------------

    public function bind(keyOrClass:*):BindingBuilder
    {
        if (keyOrClass is Class)
        {
            return bindToClass(Class(keyOrClass));
        }
        throw new Error();
    }

    public function addError(errorOrMessage:*):void
    {
        var message:Message;
        if (errorOrMessage is String)
        {
            addErrorString(String(errorOrMessage));
        }
        else if (errorOrMessage is Error)
        {
            addErrorInstance(Error(errorOrMessage));
        }
        else if (errorOrMessage is Message)
        {
            addMessage(Message(errorOrMessage));
        }
    }

    public function withSource(source:Object):Binder
    {
        var binder:BinderImpl = new BinderImpl(injector, stage);
        binder.source = source;
        return binder;
    }

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    protected function bindToType(type:Class):BindingBuilder
    {
        return new BindingBuilder(injector, type);
    }

    /*
    protected function bindToKey(key:Key):BindingBuilder
    {
    }
    */

    protected function addErrorInstance(error:Error):void
    {
        var message:String = "An exception was caught and reported. Message: " + error.message;
        addMessage(new Message(message, source, error));
    }

    protected function addErrorString(message:String):void
    {
        addMessage(new Message(message, source, null));
    }

    protected function addMessage(message:Message):void
    {
        elements.push(message);
    }

}

}
